import junit.framework.TestCase;
import java.util.*;

public class SortedBlocksTest extends TestCase {
	
	private SortedBlocks sorted;
	Iterator <Block> udList;
	Iterator <Block> lrList;
	Block block0, block1, block2, block3, block4, block5, block6, block7, block8;
	
	public void setUp ( ) {
		sorted = new SortedBlocks ( );
	
		block0 = new Block (2, 2, 0, 1);
		block1 = (new Block (2, 2, 1, 3));
		block2 = (new Block (2, 2, 2, 7));
		block3 = (new Block (2, 2, 4, 1));
		block4 = (new Block (2, 2, 4, 5));
		block5 = (new Block (2, 3, 6, 3));
		block6 = (new Block (2, 2, 8, 7));
		block7 = (new Block (2, 2, 9, 1));
		block8 = (new Block (1, 2, 9, 4));
		
		sorted.add (block0);
		sorted.add (block8);
		sorted.add (block5);
		sorted.add (block2);
		sorted.add (block3);
		sorted.add (block4);
		sorted.add (block6);
		sorted.add (block7);
		sorted.add (block1);
	}
	
	public void testAdd ( ) {
		udList = sorted.getUDSort ( );
		int [ ] order = {0, 1, 2, 4, 4, 6, 8, 9, 9};
		for (int coord : order)
			assertEquals (coord, udList.next ( ).getCoordinates ( )[0]);
		lrList = sorted.getLRSort ( );
		int [ ] order2 = {1, 1, 1, 3, 3, 4, 5, 7, 7};
		for (int coord : order2)
			assertEquals (coord, lrList.next ( ).getCoordinates ( )[1]);
	}
	
	public void testGetIndex ( ) {
		assertEquals (0, sorted.getIndex(block0, sorted.myUDSort));
		assertEquals (1, sorted.getIndex(block1, sorted.myUDSort));
		assertEquals (2, sorted.getIndex(block2, sorted.myUDSort));
		assertEquals (3, sorted.getIndex(block3, sorted.myUDSort));
		assertEquals (4, sorted.getIndex(block4, sorted.myUDSort));
		assertEquals (5, sorted.getIndex(block5, sorted.myUDSort));
		assertEquals (6, sorted.getIndex(block6, sorted.myUDSort));
		assertEquals (7, sorted.getIndex(block7, sorted.myUDSort));
		assertEquals (8, sorted.getIndex(block8, sorted.myUDSort));
		
		assertEquals (0, sorted.getIndex(block0, sorted.myLRSort));
		assertEquals (1, sorted.getIndex(block3, sorted.myLRSort));
		assertEquals (2, sorted.getIndex(block7, sorted.myLRSort));
		assertEquals (3, sorted.getIndex(block1, sorted.myLRSort));
		assertEquals (4, sorted.getIndex(block5, sorted.myLRSort));
		assertEquals (5, sorted.getIndex(block8, sorted.myLRSort));
		assertEquals (6, sorted.getIndex(block4, sorted.myLRSort));
		assertEquals (7, sorted.getIndex(block2, sorted.myLRSort));
		assertEquals (8, sorted.getIndex(block6, sorted.myLRSort));
		lrList = sorted.getLRSort();
		while (lrList.hasNext())
			System.out.println (lrList.next());
	}
	
	public void testReplace ( ) {
		sorted.replace (block5, new Block (2, 3, 3, 8));
		udList = sorted.getUDSort ( );
		int [ ] order = {0, 1, 2, 3, 4, 4, 8, 9, 9};
		for (int coord : order)
			assertEquals (coord, udList.next ( ).getCoordinates ( )[0]);
		lrList = sorted.getLRSort ( );
		int [ ] order2 = {1, 1, 1, 3, 4, 5, 7, 7, 8};
		for (int coord : order2)
			assertEquals (coord, lrList.next ( ).getCoordinates ( )[1]);
	}
	
	
	
}
